const express = require('express');
const cartRouter = express.Router();
const SQLQuery = require('../utils/query.js');



cartRouter.post('/mini/add', async function (req, res) {
    try {
        let queryObj = req.body;
        let sql = `insert into wx_cart(uid,goodsId,goodsNum) values(?,?,?)`;
        let sqlParams = [queryObj.uid, queryObj.goodsId, queryObj.goodsNum];
        let result = await SQLQuery.query(sql, sqlParams)
        res.send(global.common.resJson(0, 'success', result[0]))
    } catch (err) {
        res.send(err)
    }
})

cartRouter.post('/mini/list', async function (req, res) {
    try {
        let queryObj = req.body;
        let sql = `select * from  wx_cart where uid = ${queryObj.uid}`;

        let result = await SQLQuery.query(sql)
        for (let item of result) {
            let sql2 = `select * from  wx_goods where id = ${item.goodsId}`;
            let result2 = await SQLQuery.query(sql2);
            item.goods = result2[0];
        }
        res.send(global.common.resJson(0, 'success', result))
    } catch (err) {
        res.send(err)
    }
})



module.exports = cartRouter;